﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Volume_Resource_Management
{
    public partial class frmRP : Form
    {
        List<string> systemInformation = new List<string>();

        bool loaded = false;
        string currentYear;
        int tick = 0;

        List<string> t1OrigHrs = new List<string>();
        List<string> t2OrigHrs = new List<string>();
        List<string> t3OrigHrs = new List<string>();

        List<string> t1SalesForcast = new List<string>();
        List<string> t2SalesForcast = new List<string>();
        List<string> t3SalesForcast = new List<string>();

        List<string> t1ActualHours = new List<string>();
        List<string> t2ActualHours = new List<string>();
        List<string> t3ActualHours = new List<string>();

        List<string> t1HolidayLimit = new List<string>();
        List<string> t2HolidayLimit = new List<string>();
        List<string> t3HolidayLimit = new List<string>();

        #region Dynamic Textboxes

            Label[] tertial1Weeks = new Label[18];
            TextBox[] tertial1Hours = new TextBox[18];
            TextBox[] tertial1WeeklyVolume = new TextBox[18];
            TextBox[] tertial1PredProductivity = new TextBox[18];
            TextBox[] tertial1ActHours = new TextBox[18];
            TextBox[] tertial1ActProductivity = new TextBox[18];
            CheckBox[] tertial1HolidayLimit = new CheckBox[18];

            Label[] tertial2Weeks = new Label[18];
            TextBox[] tertial2Hours = new TextBox[18];
            TextBox[] tertial2WeeklyVolume = new TextBox[18];
            TextBox[] tertial2PredProductivity = new TextBox[18];
            TextBox[] tertial2ActHours = new TextBox[18];
            TextBox[] tertial2ActProductivity = new TextBox[18];
            CheckBox[] tertial2HolidayLimit = new CheckBox[18];

            Label[] tertial3Weeks = new Label[18];
            TextBox[] tertial3Hours = new TextBox[18];
            TextBox[] tertial3WeeklyVolume = new TextBox[18];
            TextBox[] tertial3PredProductivity = new TextBox[18];
            TextBox[] tertial3ActHours = new TextBox[18];
            TextBox[] tertial3ActProductivity = new TextBox[18];
            CheckBox[] tertial3HolidayLimit = new CheckBox[18];

        #endregion

        int storeNumber;

        public frmRP(int storeNo, string year)
        {
            InitializeComponent();
            storeNumber = storeNo;
            currentYear = year;
        }

        private void frmRP_Load(object sender, EventArgs e)
        {
            setLocations();
            getData();
            setSystemInformation();
            generateWeekLabels();
            generateHours();
            generateWeeklyVolume();
            generatePredProductivity();
            generateActHours();
            generateActProductivity();
            generateHolidayLimitWeeks();
            calcOverSpend();

            if (checkForForcast() == true)
                timer1.Start();

            button4.Text = (Convert.ToInt32(currentYear) + 1) + " >>";

            loaded = true;
        }

        
        #region Actual Hours Events

        private void tertial1ActHours_TextChanged(object sender, EventArgs e)
        {
            if (loaded)
            {
                int i = Convert.ToInt32(((TextBox)sender).Tag.ToString());

                if (tertial1ActHours[i].Text != "")
                {
                    calcActProductivity(i);
                }
            }
        }

        private void tertial2ActHours_TextChanged(object sender, EventArgs e)
        {
            if (loaded)
            {
                int i = Convert.ToInt32(((TextBox)sender).Tag.ToString());

                if (tertial2ActHours[i].Text != "")
                {
                    calcActProductivity(i);
                }
            }
        }

        private void tertial3ActHours_TextChanged(object sender, EventArgs e)
        {
            if (loaded)
            {
                int i = Convert.ToInt32(((TextBox)sender).Tag.ToString());

                if (tertial3ActHours[i].Text != "")
                {
                    calcActProductivity(i);
                }
            }
        }

        #endregion

        #region Generated Hours Events

        private void tertial1Hours_TextChanged(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(((TextBox)sender).Tag.ToString());

            if (loaded)
            {
                if (tertial1Hours[i].Text != "")
                {
                    calcOverSpend();
                    calcProductivity(i);
                }
            }
        }

        private void tertial2Hours_TextChanged(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(((TextBox)sender).Tag.ToString());

            if (loaded)
            {
                if (tertial2Hours[i].Text != "")
                {
                    calcOverSpend();
                    calcProductivity(i);
                }
            }
        }

        private void tertial3Hours_TextChanged(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(((TextBox)sender).Tag.ToString());

            if (loaded)
            {
                if (tertial3Hours[i].Text != "")
                {
                    calcOverSpend();
                    calcProductivity(i);
                }
            }
        }

        #endregion

        public void calcOverSpend()
        {
            int t1 = 0;
            int t2 = 0;
            int t3 = 0;

            for (int i = 0; i < 18; i++)
            {
                t1 += Convert.ToInt32(Math.Floor(Convert.ToDouble(tertial1Hours[i].Text)));

                if(i != 17)
                    t2 += Convert.ToInt32(Math.Floor(Convert.ToDouble(tertial2Hours[i].Text)));

                t3 += Convert.ToInt32(Math.Floor(Convert.ToDouble(tertial3Hours[i].Text)));
            }

            if (Convert.ToInt32(textBox6.Text) >= t1)
            {
                textBox6.BackColor = Color.LightGreen;
                textBox6.ForeColor = Color.Black;
            }
            else
            {
                textBox6.BackColor = Color.Red;
                textBox6.ForeColor = Color.White;
            }

            if (Convert.ToInt32(textBox7.Text) >= t2)
            {
                textBox7.BackColor = Color.LightGreen;
                textBox7.ForeColor = Color.Black;
            }
            else
            {
                textBox7.BackColor = Color.Red;
                textBox7.ForeColor = Color.White;
            }

            if (Convert.ToInt32(textBox8.Text) >= t3)
            {
                textBox8.BackColor = Color.LightGreen;
                textBox8.ForeColor = Color.Black;
            }
            else
            {
                textBox8.BackColor = Color.Red;
                textBox8.ForeColor = Color.White;
            }
        }

        public void calcActProductivity(int tbNo)
        {
            tertial1ActProductivity[tbNo].Text = Math.Round(Convert.ToDouble(tertial1WeeklyVolume[tbNo].Text) /
                    Convert.ToDouble(tertial1ActHours[tbNo].Text), 2).ToString();

            if (Convert.ToDouble(tertial1ActProductivity[tbNo].Text) < 1.28)
            {
                tertial1ActProductivity[tbNo].BackColor = Color.Red;
                tertial1ActProductivity[tbNo].ForeColor = Color.White;
            }
            else
            {
                tertial1ActProductivity[tbNo].BackColor = Color.LightGreen;
                tertial1ActProductivity[tbNo].ForeColor = Color.Black;
            }

            if (tbNo != 17)
            {
                tertial2ActProductivity[tbNo].Text = Math.Round(Convert.ToDouble(tertial2WeeklyVolume[tbNo].Text) /
                        Convert.ToDouble(tertial2ActHours[tbNo].Text), 2).ToString();

                if (Convert.ToDouble(tertial2ActProductivity[tbNo].Text) < 1.28)
                {
                    tertial2ActProductivity[tbNo].BackColor = Color.Red;
                    tertial2ActProductivity[tbNo].ForeColor = Color.White;
                }
                else
                {
                    tertial2ActProductivity[tbNo].BackColor = Color.LightGreen;
                    tertial2ActProductivity[tbNo].ForeColor = Color.Black;
                }
            }

            tertial3ActProductivity[tbNo].Text = Math.Round(Convert.ToDouble(tertial3WeeklyVolume[tbNo].Text) /
                   Convert.ToDouble(tertial3ActHours[tbNo].Text), 2).ToString();

            if (Convert.ToDouble(tertial3ActProductivity[tbNo].Text) < 1.28)
            {
                tertial3ActProductivity[tbNo].BackColor = Color.Red;
                tertial3ActProductivity[tbNo].ForeColor = Color.White;
            }
            else
            {
                tertial3ActProductivity[tbNo].BackColor = Color.LightGreen;
                tertial3ActProductivity[tbNo].ForeColor = Color.Black;
            }
        }

        public void calcProductivity(int tbNo)
        {
            tertial1PredProductivity[tbNo].Text = Math.Round(Convert.ToDouble(tertial1WeeklyVolume[tbNo].Text) /
                    Convert.ToDouble(tertial1Hours[tbNo].Text), 2).ToString();

            if (Convert.ToDouble(tertial1PredProductivity[tbNo].Text) < 1.28)
            {
                tertial1PredProductivity[tbNo].BackColor = Color.Red;
                tertial1PredProductivity[tbNo].ForeColor = Color.White;
            }
            else
            {
                tertial1PredProductivity[tbNo].BackColor = Color.LightGreen;
                tertial1PredProductivity[tbNo].ForeColor = Color.Black;
            }

            if (tbNo != 17)
            {
                tertial2PredProductivity[tbNo].Text = Math.Round(Convert.ToDouble(tertial2WeeklyVolume[tbNo].Text) /
                        Convert.ToDouble(tertial2Hours[tbNo].Text), 2).ToString();

                if (Convert.ToDouble(tertial2PredProductivity[tbNo].Text) < 1.28)
                {
                    tertial2PredProductivity[tbNo].BackColor = Color.Red;
                    tertial2PredProductivity[tbNo].ForeColor = Color.White;
                }
                else
                {
                    tertial2PredProductivity[tbNo].BackColor = Color.LightGreen;
                    tertial2PredProductivity[tbNo].ForeColor = Color.Black;
                }
            }

            tertial3PredProductivity[tbNo].Text = Math.Round(Convert.ToDouble(tertial3WeeklyVolume[tbNo].Text) /
                    Convert.ToDouble(tertial3Hours[tbNo].Text), 2).ToString();

            if (Convert.ToDouble(tertial3PredProductivity[tbNo].Text) < 1.28)
            {
                tertial3PredProductivity[tbNo].BackColor = Color.Red;
                tertial3PredProductivity[tbNo].ForeColor = Color.White;
            }
            else
            {
                tertial3PredProductivity[tbNo].BackColor = Color.LightGreen;
                tertial3PredProductivity[tbNo].ForeColor = Color.Black;
            }
        }

        #region Holiday Limit Check Events

            private void tertial1HolidayLimit_CheckChanged(object sender, EventArgs e)
            {
                int i = Convert.ToInt32(((CheckBox)sender).Tag.ToString());

                if (t1HolidayLimit[i] == "1")
                {
                    t1HolidayLimit[i] = "0";
                }
                else
                {
                    t1HolidayLimit[i] = "1";
                }
            }

            private void tertial2HolidayLimit_CheckChanged(object sender, EventArgs e)
            {
                int i = Convert.ToInt32(((CheckBox)sender).Tag.ToString());

                if (t2HolidayLimit[i] == "1")
                {
                    t2HolidayLimit[i] = "0";
                }
                else
                {
                    t2HolidayLimit[i] = "1";
                }
            }

            private void tertial3HolidayLimit_CheckChanged(object sender, EventArgs e)
            {
                int i = Convert.ToInt32(((CheckBox)sender).Tag.ToString());

                if (t3HolidayLimit[i] == "1")
                {
                    t3HolidayLimit[i] = "0";
                }
                else
                {
                    t3HolidayLimit[i] = "1";
                }
            }

        #endregion

        public void generateHolidayLimitWeeks()
        {
            for (int i = 0; i < 18; i++)
            {
                tertial1HolidayLimit[i] = new CheckBox();
                tertial2HolidayLimit[i] = new CheckBox();
                tertial3HolidayLimit[i] = new CheckBox();

                tertial1HolidayLimit[i].Top = tertial2HolidayLimit[i].Top = tertial3HolidayLimit[i].Top = tertial1ActProductivity[i].Top + tertial1ActProductivity[i].Height + 5;
                tertial1HolidayLimit[i].Width = tertial2HolidayLimit[i].Width = tertial3HolidayLimit[i].Width = tertial1ActProductivity[i].Width;
                tertial1HolidayLimit[i].Left = tertial2HolidayLimit[i].Left = tertial3HolidayLimit[i].Left = tertial1ActProductivity[i].Left;
                tertial1HolidayLimit[i].CheckAlign = tertial2HolidayLimit[i].CheckAlign = tertial3HolidayLimit[i].CheckAlign = ContentAlignment.MiddleCenter;
                tertial1HolidayLimit[i].Tag = tertial2HolidayLimit[i].Tag = tertial3HolidayLimit[i].Tag = i;

                tertial1HolidayLimit[i].CheckedChanged += tertial1HolidayLimit_CheckChanged;
                tertial2HolidayLimit[i].CheckedChanged += tertial2HolidayLimit_CheckChanged;
                tertial3HolidayLimit[i].CheckedChanged += tertial3HolidayLimit_CheckChanged;

                if (t1HolidayLimit[i] == "1")
                {
                    tertial1HolidayLimit[i].Checked = true;
                }
                grpT1.Controls.Add(tertial1HolidayLimit[i]);

                if (i != 17)
                {
                    if (t2HolidayLimit[i] == "1")
                    {
                        tertial2HolidayLimit[i].Checked = true;
                    }
                    grpT2.Controls.Add(tertial2HolidayLimit[i]);
                }

                if (t3HolidayLimit[i] == "1")
                {
                    tertial3HolidayLimit[i].Checked = true;
                }
                grpT3.Controls.Add(tertial3HolidayLimit[i]);

            }
            label20.Top = label21.Top = label27.Top = tertial1HolidayLimit[1].Top + 3;
        }

        public void generateActProductivity()
        {
            for (int i = 0; i < 18; i++)
            {
                tertial1ActProductivity[i] = new TextBox();
                tertial2ActProductivity[i] = new TextBox();
                tertial3ActProductivity[i] = new TextBox();

                tertial1ActProductivity[i].Top = tertial2ActProductivity[i].Top = tertial3ActProductivity[i].Top =
                    tertial1ActHours[i].Top + tertial1ActHours[i].Height + 5;
                tertial1ActProductivity[i].Width = tertial2ActProductivity[i].Width = tertial3ActProductivity[i].Width = tertial1ActHours[i].Width;
                tertial1ActProductivity[i].Left = tertial2ActProductivity[i].Left = tertial3ActProductivity[i].Left = tertial1ActHours[i].Left;
                tertial1ActProductivity[i].TextAlign = tertial2ActProductivity[i].TextAlign = tertial3ActProductivity[i].TextAlign =
                    HorizontalAlignment.Center;
                tertial1ActProductivity[i].Font = tertial2ActProductivity[i].Font = tertial3ActProductivity[i].Font = new Font("Calibri", 8);
                tertial1ActProductivity[i].ReadOnly = tertial2ActProductivity[i].ReadOnly = tertial3ActProductivity[i].ReadOnly = true;

                grpT1.Controls.Add(tertial1ActProductivity[i]);
                
                if(i != 17)
                {
                    grpT2.Controls.Add(tertial2ActProductivity[i]);
                }

                grpT3.Controls.Add(tertial3ActProductivity[i]);

                calcActProductivity(i);
            }
            label19.Top = label22.Top = label28.Top = tertial1ActProductivity[1].Top + 3;
        }

        public void generateActHours()
        {
            for (int i = 0; i < 18; i++)
            {
                tertial1ActHours[i] = new TextBox();
                tertial2ActHours[i] = new TextBox();
                tertial3ActHours[i] = new TextBox();

                tertial1ActHours[i].Top = tertial2ActHours[i].Top = tertial3ActHours[i].Top =
                    (tertial1PredProductivity[i].Top + tertial1PredProductivity[i].Height + 5);
                tertial1ActHours[i].Width = tertial2ActHours[i].Width = tertial3ActHours[i].Width = tertial1PredProductivity[i].Width;
                tertial1ActHours[i].Left = tertial2ActHours[i].Left = tertial3ActHours[i].Left = tertial1PredProductivity[i].Left;
                tertial1ActHours[i].TextAlign = tertial2ActHours[i].TextAlign = tertial3ActHours[i].TextAlign = HorizontalAlignment.Center;
                tertial1ActHours[i].Font = tertial2ActHours[i].Font = tertial3ActHours[i].Font = new Font("Calibri", 8);
                tertial1ActHours[i].MaxLength = tertial2ActHours[i].MaxLength = tertial3ActHours[i].MaxLength = 4;
                tertial1ActHours[i].Tag = tertial2ActHours[i].Tag = tertial3ActHours[i].Tag = i;

                tertial1ActHours[i].TextChanged += tertial1ActHours_TextChanged;
                tertial2ActHours[i].TextChanged += tertial2ActHours_TextChanged;
                tertial3ActHours[i].TextChanged += tertial3ActHours_TextChanged;

                tertial1ActHours[i].Text = t1ActualHours[i];
                grpT1.Controls.Add(tertial1ActHours[i]);

                if (i != 17)
                {
                    tertial2ActHours[i].Text = t2ActualHours[i];
                    grpT2.Controls.Add(tertial2ActHours[i]);
                }

                tertial3ActHours[i].Text = t3ActualHours[i];
                grpT3.Controls.Add(tertial3ActHours[i]);
            }
            label18.Top = label23.Top = label29.Top = tertial1ActHours[1].Top + 3;
        }

        public void generatePredProductivity()
        {
            for (int i = 0; i < 18; i++)
            {
                tertial1PredProductivity[i] = new TextBox();
                tertial2PredProductivity[i] = new TextBox();
                tertial3PredProductivity[i] = new TextBox();

                tertial1PredProductivity[i].Top = tertial2PredProductivity[i].Top = tertial3PredProductivity[i].Top =
                    tertial1WeeklyVolume[i].Top + tertial1WeeklyVolume[i].Height + 5;
                tertial1PredProductivity[i].Width = tertial2PredProductivity[i].Width = tertial3PredProductivity[i].Width =
                    tertial1WeeklyVolume[i].Width;
                tertial1PredProductivity[i].Left = tertial2PredProductivity[i].Left = tertial3PredProductivity[i].Left = tertial1WeeklyVolume[i].Left;
                tertial1PredProductivity[i].TextAlign = tertial2PredProductivity[i].TextAlign = tertial3PredProductivity[i].TextAlign =
                    HorizontalAlignment.Center;
                tertial1PredProductivity[i].Font = tertial2PredProductivity[i].Font = tertial3PredProductivity[i].Font = new Font("Calibri", 8);
                tertial1PredProductivity[i].ReadOnly = tertial2PredProductivity[i].ReadOnly = tertial3PredProductivity[i].ReadOnly = true;

                calcProductivity(i);

                grpT1.Controls.Add(tertial1PredProductivity[i]);

                if (i != 17)
                    grpT2.Controls.Add(tertial2PredProductivity[i]);

                grpT3.Controls.Add(tertial3PredProductivity[i]);
            }

            label17.Top = label24.Top = label30.Top = tertial1PredProductivity[1].Top + 3;
        }

        public void generateWeeklyVolume()
        {
            for (int i = 0; i < 18; i++)
            {
                tertial1WeeklyVolume[i] = new TextBox();
                tertial2WeeklyVolume[i] = new TextBox();
                tertial3WeeklyVolume[i] = new TextBox();

                tertial1WeeklyVolume[i].Top = tertial2WeeklyVolume[i].Top = tertial3WeeklyVolume[i].Top =
                    tertial1Hours[i].Top + tertial1Hours[i].Height + 5;
                tertial1WeeklyVolume[i].Width = tertial2WeeklyVolume[i].Width = tertial3WeeklyVolume[i].Width = tertial1Hours[i].Width;
                tertial1WeeklyVolume[i].Left = tertial2WeeklyVolume[i].Left = tertial3WeeklyVolume[i].Left = tertial1Hours[i].Left;
                tertial1WeeklyVolume[i].TextAlign = tertial2WeeklyVolume[i].TextAlign = tertial3WeeklyVolume[i].TextAlign = HorizontalAlignment.Center;
                tertial1WeeklyVolume[i].Font = tertial2WeeklyVolume[i].Font = tertial3WeeklyVolume[i].Font = new Font("Calibri", 8);
                tertial1WeeklyVolume[i].ReadOnly = tertial2WeeklyVolume[i].ReadOnly = tertial3WeeklyVolume[i].ReadOnly = true;

                calcVolume(i);
 
                grpT1.Controls.Add(tertial1WeeklyVolume[i]);

                if(i != 17)
                {
                    
                    grpT2.Controls.Add(tertial2WeeklyVolume[i]);
                }
                
                grpT3.Controls.Add(tertial3WeeklyVolume[i]);
            }
            label16.Top = label25.Top = label31.Top = tertial1WeeklyVolume[1].Top + 3;
        }

        public void calcVolume(int i)
        {
            tertial1WeeklyVolume[i].Text = Math.Floor((Convert.ToDouble(t1SalesForcast[i]) * 0.962) / Convert.ToInt32(txtPriceM3.Text)).ToString();

            if(i != 17)
                tertial2WeeklyVolume[i].Text = Math.Floor((Convert.ToDouble(t2SalesForcast[i]) * 0.962)
                        / Convert.ToInt32(txtPriceM3.Text)).ToString();

            tertial3WeeklyVolume[i].Text = Math.Floor((Convert.ToDouble(t3SalesForcast[i]) * 0.962) / Convert.ToInt32(txtPriceM3.Text)).ToString();
        }

        public void generateHours()
        {
            for (int i = 0; i < 18; i++)
            {
                tertial1Hours[i] = new TextBox();
                tertial2Hours[i] = new TextBox();
                tertial3Hours[i] = new TextBox();

                tertial1Hours[i].Top = tertial2Hours[i].Top = tertial3Hours[i].Top = 40;
                tertial1Hours[i].Width = tertial2Hours[i].Width = tertial3Hours[i].Width = tertial1Weeks[i].Width;
                tertial1Hours[i].Left = tertial2Hours[i].Left = tertial3Hours[i].Left = tertial1Weeks[i].Left;
                tertial1Hours[i].TextAlign = tertial2Hours[i].TextAlign = tertial3Hours[i].TextAlign = HorizontalAlignment.Center;
                tertial1Hours[i].Font = tertial2Hours[i].Font = tertial3Hours[i].Font = new Font("Calibri", 8);
                tertial1Hours[i].Tag = tertial2Hours[i].Tag = tertial3Hours[i].Tag = i;
                tertial1Hours[i].MaxLength = tertial2Hours[i].MaxLength = tertial3Hours[i].MaxLength = 4;

                tertial1Hours[i].TextChanged += tertial1Hours_TextChanged;
                tertial2Hours[i].TextChanged += tertial2Hours_TextChanged;
                tertial3Hours[i].TextChanged += tertial3Hours_TextChanged;

                tertial1Hours[i].Text = Math.Floor(Convert.ToDouble(t1OrigHrs[i]) * (Convert.ToDouble(textBox2.Text) / 100)).ToString();
                grpT1.Controls.Add(tertial1Hours[i]);

                if (i != 17)
                {
                    tertial2Hours[i].Text = Math.Floor(Convert.ToDouble(t2OrigHrs[i]) * (Convert.ToDouble(textBox2.Text) / 100)).ToString();
                    grpT2.Controls.Add(tertial2Hours[i]);
                }

                tertial3Hours[i].Text = Math.Floor(Convert.ToDouble(t3OrigHrs[i]) * (Convert.ToDouble(textBox2.Text) / 100)).ToString();
                grpT3.Controls.Add(tertial3Hours[i]);
            }

            label15.Top = label26.Top = label32.Top = tertial1Hours[1].Top + 3;

        }

        public void generateWeekLabels()
        {
            int t1 = 35;
            int t2 = 1;
            int t3 = 18;

            for (int i = 0; i < 18; i++)
            {
                tertial1Weeks[i] = new Label();
                tertial2Weeks[i] = new Label();
                tertial3Weeks[i] = new Label();

                tertial1Weeks[i].Top = tertial2Weeks[i].Top = tertial3Weeks[i].Top = 15;
                tertial1Weeks[i].Width = tertial2Weeks[i].Width = tertial3Weeks[i].Width = (grpT1.Width - 65) / 18;
                tertial1Weeks[i].Left = tertial2Weeks[i].Left = tertial3Weeks[i].Left = (tertial1Weeks[i].Width * i) + 60;
                tertial1Weeks[i].TextAlign = tertial2Weeks[i].TextAlign = tertial3Weeks[i].TextAlign = ContentAlignment.MiddleCenter;
                tertial1Weeks[i].Font = tertial2Weeks[i].Font = tertial3Weeks[i].Font = new Font("Calibri", 8);

                tertial1Weeks[i].Text = t1.ToString();
                grpT1.Controls.Add(tertial1Weeks[i]);

                if (i != 17)
                {
                    tertial2Weeks[i].Text = t2.ToString();
                    grpT2.Controls.Add(tertial2Weeks[i]);
                }

                tertial3Weeks[i].Text = t3.ToString();
                grpT3.Controls.Add(tertial3Weeks[i]);

                t1++;
                t2++;
                t3++;

            }
        }

        public void calculateTertialHours()
        {
            textBox6.Text = Math.Floor((Convert.ToDouble(textBox1.Text) * (Convert.ToDouble(textBox4.Text) / 100))
                * (Convert.ToDouble(textBox2.Text) / 100)).ToString();

            textBox7.Text = Math.Floor((Convert.ToDouble(textBox1.Text) * (Convert.ToDouble(textBox3.Text) / 100))
                * (Convert.ToDouble(textBox2.Text) / 100)).ToString();

            textBox8.Text = Math.Floor((Convert.ToDouble(textBox1.Text) * (Convert.ToDouble(textBox5.Text) / 100))
                * (Convert.ToDouble(textBox2.Text) / 100)).ToString();
        }

        public void setSystemInformation()
        {
            txtSalesGoal.Text = systemInformation[0];
            txtCDShare.Text = systemInformation[1];
            txtProdIndex.Text = systemInformation[2];
            txtPriceM3.Text = systemInformation[3];
            textBox1.Text = systemInformation[4];
            textBox2.Text = systemInformation[5];

            calculateTertialHours();
        }

        private void getData()
        {
            systemInformation = databaseAccessability.getSystemInformation(storeNumber, Convert.ToInt32(currentYear));

            t1OrigHrs = databaseAccessability.getOrigHours(storeNumber, 1, Convert.ToInt32(currentYear));
            t2OrigHrs = databaseAccessability.getOrigHours(storeNumber, 2, Convert.ToInt32(currentYear));
            t3OrigHrs = databaseAccessability.getOrigHours(storeNumber, 3, Convert.ToInt32(currentYear));

            t1SalesForcast = databaseAccessability.getSalesForcast(storeNumber, 1, Convert.ToInt32(currentYear));
            t2SalesForcast = databaseAccessability.getSalesForcast(storeNumber, 2, Convert.ToInt32(currentYear));
            t3SalesForcast = databaseAccessability.getSalesForcast(storeNumber, 3, Convert.ToInt32(currentYear));

            t1ActualHours = databaseAccessability.getActualHours(storeNumber, 1, Convert.ToInt32(currentYear));
            t2ActualHours = databaseAccessability.getActualHours(storeNumber, 2, Convert.ToInt32(currentYear));
            t3ActualHours = databaseAccessability.getActualHours(storeNumber, 3, Convert.ToInt32(currentYear));

            t1HolidayLimit = databaseAccessability.getHolidayLimit(storeNumber, Convert.ToInt32(currentYear), 1);
            t2HolidayLimit = databaseAccessability.getHolidayLimit(storeNumber, Convert.ToInt32(currentYear), 2);
            t3HolidayLimit = databaseAccessability.getHolidayLimit(storeNumber, Convert.ToInt32(currentYear), 3);
        }

        private bool checkForForcast()
        {
            for (int i = 0; i < 18; i++)
            {
                if (t1SalesForcast[i] == "0")
                {
                    return true;
                }

                if (i != 17)
                {
                    if (t2SalesForcast[i] == "0")
                    {
                        return true;
                    }
                }
                
                    if (t3SalesForcast[i] == "0")
                {
                    return true;
                }
            }
            return false;
        }

        private void setLocations()
        {
            grpTotHours.Top = (this.ClientSize.Height - grpTotHours.Height) / 2;
            grpScenario.Top = grpTertial2.Top = groupBox2.Top = (this.ClientSize.Height - grpScenario.Height) / 2;

            grpT1.Height = grpT2.Height = grpT3.Height = (this.ClientSize.Height / 3) - 30;
            grpT2.Top = (this.ClientSize.Height - grpT2.Height) / 2;
            grpT1.Top = grpT2.Top - grpT1.Height;
            grpT3.Top = grpT2.Top + grpT2.Height;

            grpTertial1.Top = groupBox1.Top = (grpT1.Top + (grpT1.Height / 2) - (grpTertial1.Height / 2));
            grpTertial3.Top = groupBox3.Top = (grpT3.Top + (grpT3.Height / 2) - (grpTertial3.Height / 2));

            label1.Top = label3.Top = label6.Top = label9.Top = (this.ClientSize.Height - label1.Height) / 2;
            label2.Top = label5.Top = label8.Top = (grpTertial1.Top + (grpTertial1.Height / 2)) - label2.Height / 2;
            label4.Top = label7.Top = label10.Top = (grpTertial3.Top + (grpTertial3.Height / 2)) - label4.Height / 2;

            groupBox4.Top = grpTotHours.Top + grpTotHours.Height + 5;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (loaded)
            {
                if (textBox2.Text != "")
                {
                    if (Convert.ToInt32(textBox2.Text) <= 100)
                    {
                        for (int i = 0; i < 18; i++)
                        {
                            tertial1Hours[i].Text = Math.Floor(Convert.ToDouble(t1OrigHrs[i]) * (Convert.ToDouble(textBox2.Text) / 100)).ToString();
                            
                            if(i != 17)
                                tertial2Hours[i].Text = Math.Floor(Convert.ToDouble(t2OrigHrs[i]) * (Convert.ToDouble(textBox2.Text) / 100)).ToString();


                            tertial3Hours[i].Text = Math.Floor(Convert.ToDouble(t3OrigHrs[i]) * (Convert.ToDouble(textBox2.Text) / 100)).ToString();

                            calcProductivity(i);
                            calculateTertialHours();
                        }
                    }
                    else
                    {
                        textBox2.Text = "100";
                    }
                }
            }
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            groupBox4.Visible = !groupBox4.Visible;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            double salesGoal = Convert.ToDouble(txtSalesGoal.Text);
            double cdShare = Convert.ToDouble(txtCDShare.Text);
            double prodIndex = Convert.ToDouble(txtProdIndex.Text);
            double ppm = Convert.ToDouble(txtPriceM3.Text);

            cdShare = (100 - cdShare) / 100;

            double actSalesGoal = salesGoal * cdShare;
            double totalCube = actSalesGoal / ppm;

            int hours = Convert.ToInt32(totalCube / prodIndex);
            textBox1.Text = hours.ToString();

            generateTertialTotHours();

            groupBox4.Visible = false;
        }

        private void generateTertialTotHours()
        {
            textBox6.Text = Math.Floor((Convert.ToDouble(textBox1.Text) * (Convert.ToDouble(textBox4.Text) / 100))* (Convert.ToDouble(textBox2.Text) / 100)).ToString();
            textBox7.Text = Math.Floor((Convert.ToDouble(textBox1.Text) * (Convert.ToDouble(textBox3.Text) / 100)) * (Convert.ToDouble(textBox2.Text) / 100)).ToString();
            textBox8.Text = Math.Floor((Convert.ToDouble(textBox1.Text) * (Convert.ToDouble(textBox5.Text) / 100)) * (Convert.ToDouble(textBox2.Text) / 100)).ToString();

            double t1 = Math.Floor((Convert.ToDouble(textBox1.Text) * (Convert.ToDouble(textBox4.Text) / 100) / 18));
            double t2 = Math.Floor((Convert.ToDouble(textBox1.Text) * (Convert.ToDouble(textBox3.Text) / 100) / 17));
            double t3 = Math.Floor((Convert.ToDouble(textBox1.Text) * (Convert.ToDouble(textBox5.Text) / 100) / 18));

            for (int i = 0; i < 18; i++)
            {
                t1OrigHrs[i] = t1.ToString();
                tertial1Hours[i].Text = t1.ToString();

                if (i != 17)
                {
                    t2OrigHrs[i] = t2.ToString();
                    tertial2Hours[i].Text = t2.ToString();
                }

                t3OrigHrs[i] = t3.ToString();
                tertial3Hours[i].Text = t3.ToString();

                calcVolume(i);
                calcProductivity(i);
            }
            calcOverSpend();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 18; i++)
            {
                if (textBox2.Text == "100")
                {
                    t1OrigHrs[i] = tertial1Hours[i].Text;

                    if(i != 17)
                        t2OrigHrs[i] = tertial2Hours[i].Text;
                    
                    t3OrigHrs[i] = tertial3Hours[i].Text;
                }
                else
                {
                    double perc = (Convert.ToDouble(textBox2.Text) / 100);

                    t1OrigHrs[i] = Math.Floor(Convert.ToInt32(tertial1Hours[i].Text) / perc).ToString();
                    
                    if(i != 17)
                        t2OrigHrs[i] = Math.Floor(Convert.ToInt32(tertial2Hours[i].Text) / perc).ToString();
                    
                    t3OrigHrs[i] = Math.Floor(Convert.ToInt32(tertial3Hours[i].Text) / perc).ToString();
                }
            }

            databaseAccessability.updateSystemInformation(storeNumber, Convert.ToInt32(currentYear), Convert.ToInt32(txtSalesGoal.Text), Convert.ToInt32(txtCDShare.Text),
                Convert.ToDouble(txtProdIndex.Text), Convert.ToInt32(txtPriceM3.Text), Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));

            for (int i = 0; i < 18; i++)
            {
                databaseAccessability.updateWeeklyGoals(storeNumber, Convert.ToInt32(tertial1Weeks[i].Text), 1, Convert.ToInt32(currentYear), t1OrigHrs[i], tertial1ActHours[i].Text,
                    t1HolidayLimit[i]);

                if(i != 17)
                    databaseAccessability.updateWeeklyGoals(storeNumber, Convert.ToInt32(tertial2Weeks[i].Text), 2, Convert.ToInt32(currentYear), t2OrigHrs[i], tertial2ActHours[i].Text,
                        t2HolidayLimit[i]);

                databaseAccessability.updateWeeklyGoals(storeNumber, Convert.ToInt32(tertial3Weeks[i].Text), 3, Convert.ToInt32(currentYear), t3OrigHrs[i], tertial3ActHours[i].Text,
                    t3HolidayLimit[i]);                    
            }

            this.Hide();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //button3.PerformClick();

            if (button4.Text.Substring(0,2) == "<<")
            {
                button4.Text = (Convert.ToInt32(currentYear)) + " >>";
                currentYear = (Convert.ToInt32(currentYear) - 1).ToString();
                getNewInformation();
            }
            else
            {
                if (databaseAccessability.checkForNextYear(storeNumber, Convert.ToInt32(currentYear) + 1) == 0)
                {
                    generateFollowingYear();
                }
                
                button4.Text = "<< " + currentYear;
                currentYear = (Convert.ToInt32(currentYear) + 1).ToString();
                getNewInformation();
            }
        }

        private void getNewInformation()
        {
            timer1.Stop();
            button4.BackColor = Color.Empty;
            button4.ForeColor = Color.Black;

            getData();

            for (int i = 0; i < 18; i++)
            {
                setSystemInformation();

                tertial1Hours[i].Text = Math.Floor(Convert.ToDouble(t1OrigHrs[i]) * (Convert.ToDouble(textBox2.Text) / 100)).ToString();
                tertial1ActHours[i].Text = t1ActualHours[i];

                if (i != 17)
                {
                    tertial2Hours[i].Text = Math.Floor(Convert.ToDouble(t2OrigHrs[i]) * (Convert.ToDouble(textBox2.Text) / 100)).ToString();
                    tertial2ActHours[i].Text = t2ActualHours[i];
                }

                tertial3Hours[i].Text = Math.Floor(Convert.ToDouble(t3OrigHrs[i]) * (Convert.ToDouble(textBox2.Text) / 100)).ToString();
                tertial3ActHours[i].Text = t3ActualHours[i];

                calcVolume(i);
                calcProductivity(i);
                calcActProductivity(i);
                calcOverSpend();
            }

            if (checkForForcast() == true)
            {
                timer1.Start();
            }
            else
            {
                timer1.Stop();
                button5.BackColor = Color.Empty;
                button5.ForeColor = Color.Black;
            }
        }

        private void generateFollowingYear()
        {
            int t1 = 35;
            int t2 = 1;
            int t3 = 18;

            for (int i = 0; i < 18; i++)
            {
                databaseAccessability.createNextYear(storeNumber, Convert.ToInt32(currentYear) + 1, t1, 1);

                if(i != 17)
                    databaseAccessability.createNextYear(storeNumber, Convert.ToInt32(currentYear) + 1, t2, 2);
                
                databaseAccessability.createNextYear(storeNumber, Convert.ToInt32(currentYear) + 1, t3, 3);

                t1++;
                t2++;
                t3++;
            }

            databaseAccessability.createNextYearSystemInformation(storeNumber, Convert.ToInt32(currentYear) + 1);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (tick == 0)
            {
                button5.BackColor = Color.Red;
                button5.ForeColor = Color.White;
                tick = 1;
            }
            else
            {
                button5.BackColor = Color.Empty;
                button5.ForeColor = Color.Black;
                tick = 0;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            frmAddSalesForcast sF = new frmAddSalesForcast(storeNumber, Convert.ToInt32(currentYear));
            sF.Show();
        }

        private void frmRP_Activated(object sender, EventArgs e)
        {
            getData();

            if (checkForForcast() == true)
            {
                timer1.Start();
            }
            else
            {
                timer1.Stop();
                button5.BackColor = Color.Empty;
                button5.ForeColor = Color.Black;
            }
        }
    }
}
